package cn.occgmat.service.impl;

import cn.occgmat.entity.RbsystemTimetable;
import cn.occgmat.dao.RbsystemTimetableDao;
import cn.occgmat.service.RbsystemTimetableService;
import cn.occgmat.util.BaseResponse;
import cn.occgmat.util.StatusCode;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 车次表（时刻表）(RbsystemTimetable)表服务实现类
 *
 * @author shimmer
 * @since 2021-03-27 10:01:54
 */
@Transactional(rollbackFor = Exception.class)
@Service("rbsystemTimetableService")
public class RbsystemTimetableServiceImpl implements RbsystemTimetableService {
    @Resource
    private RbsystemTimetableDao rbsystemTimetableDao;

    /**
     * 通过ID查询单条数据
     *
     * @param timetableId 主键
     * @return 实例对象
     */
    @Override
    public RbsystemTimetable queryById(Integer timetableId) {
        return this.rbsystemTimetableDao.queryById(timetableId);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    @Override
    public BaseResponse<RbsystemTimetable> queryAllByLimit(int offset, int limit) {
        int startPage = (offset - 1) * limit;
        List<RbsystemTimetable> timetableList = this.rbsystemTimetableDao.queryAllByLimit(startPage, limit);
        BaseResponse<RbsystemTimetable> baseResponse;
        if (timetableList != null) {
            baseResponse = new BaseResponse<>(StatusCode.Success);
            baseResponse.setData(timetableList);
            baseResponse.setCount(this.rbsystemTimetableDao.getCount());
        } else {
            baseResponse = new BaseResponse<>(StatusCode.NotFound);
        }
        return baseResponse;
    }

    /**
     * 新增数据
     *
     * @param rbsystemTimetable 实例对象
     * @return 实例对象
     */
    @Override
    public RbsystemTimetable insert(RbsystemTimetable rbsystemTimetable) {
        this.rbsystemTimetableDao.insert(rbsystemTimetable);
        return rbsystemTimetable;
    }

    /**
     * 修改数据
     *
     * @param rbsystemTimetable 实例对象
     * @return 实例对象
     */
    @Override
    public BaseResponse<?> update(RbsystemTimetable rbsystemTimetable) {
        int result =  this.rbsystemTimetableDao.update(rbsystemTimetable);
        return result > 0 ? new BaseResponse<>(StatusCode.Success) : new BaseResponse<>(StatusCode.Fail);
    }

    /**
     * 通过主键删除数据
     *
     * @param timetableId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer timetableId) {
        return this.rbsystemTimetableDao.deleteById(timetableId) > 0;
    }

    /**
     * 获取总数
     *
     * @return 总数
     */
    @Override
    public Integer getCount() {
        return rbsystemTimetableDao.getCount();
    }
}
